<template>
  <div class="asset-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-button @click="goBack" type="text">
              <el-icon><component :is="iconComponents.ArrowLeft" /></el-icon>
              返回
            </el-button>
            <span class="asset-title">{{ asset.name }}</span>
            <el-tag :type="getStatusTagType(asset.status)" class="status-tag">
              {{ getStatusLabel(asset.status) }}
            </el-tag>
          </div>
          <div class="header-actions">
            <el-button type="warning" @click="showEditDialog">
              <el-icon><component :is="iconComponents.Edit" /></el-icon>
              编辑
            </el-button>
            <el-button type="danger" @click="handleDelete">
              <el-icon><component :is="iconComponents.Delete" /></el-icon>
              删除
            </el-button>
          </div>
        </div>
      </template>

      <div class="detail-content">
        <el-row :gutter="20">
          <!-- 基本信息 -->
          <el-col :span="12">
            <div class="info-section">
              <h3 class="section-title">基本信息</h3>
              <div class="info-grid">
                <div class="info-item">
                  <label>资产ID:</label>
                  <span>{{ asset.id }}</span>
                </div>
                <div class="info-item">
                  <label>资产名称:</label>
                  <span>{{ asset.name }}</span>
                </div>
                <div class="info-item">
                  <label>资产类型:</label>
                  <el-tag :type="getTypeTagType(asset.type)">
                    {{ getTypeLabel(asset.type) }}
                  </el-tag>
                </div>
                <div class="info-item">
                  <label>IP地址:</label>
                  <span>{{ asset.ip }}</span>
                </div>
                <div class="info-item">
                  <label>序列号:</label>
                  <span>{{ asset.serialNumber }}</span>
                </div>
                <div class="info-item">
                  <label>创建时间:</label>
                  <span>{{ asset.createTime }}</span>
                </div>
              </div>
            </div>
          </el-col>

          <!-- 管理信息 -->
          <el-col :span="12">
            <div class="info-section">
              <h3 class="section-title">管理信息</h3>
              <div class="info-grid">
                <div class="info-item">
                  <label>所属部门:</label>
                  <span>{{ getDepartmentLabel(asset.department) }}</span>
                </div>
                <div class="info-item">
                  <label>负责人:</label>
                  <span>{{ asset.owner }}</span>
                </div>
                <div class="info-item">
                  <label>物理位置:</label>
                  <span>{{ asset.location }}</span>
                </div>
                <div class="info-item">
                  <label>当前状态:</label>
                  <el-tag :type="getStatusTagType(asset.status)">
                    {{ getStatusLabel(asset.status) }}
                  </el-tag>
                </div>
                <div class="info-item">
                  <label>最后更新:</label>
                  <span>{{ asset.updateTime || '暂无' }}</span>
                </div>
                <div class="info-item">
                  <label>备注描述:</label>
                  <span>{{ asset.description || '暂无' }}</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 监控信息 -->
        <div class="info-section" v-if="asset.type === 'server'">
          <h3 class="section-title">监控信息</h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="monitor-card">
                <div class="monitor-icon cpu">
                  <el-icon><component :is="iconComponents.Cpu" /></el-icon>
                </div>
                <div class="monitor-content">
                  <div class="monitor-value">{{ monitorData.cpu }}%</div>
                  <div class="monitor-label">CPU使用率</div>
                </div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="monitor-card">
                <div class="monitor-icon memory">
                  <el-icon><component :is="iconComponents.Memo" /></el-icon>
                </div>
                <div class="monitor-content">
                  <div class="monitor-value">{{ monitorData.memory }}%</div>
                  <div class="monitor-label">内存使用率</div>
                </div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="monitor-card">
                <div class="monitor-icon disk">
                  <el-icon><component :is="iconComponents.FolderOpened" /></el-icon>
                </div>
                <div class="monitor-content">
                  <div class="monitor-value">{{ monitorData.disk }}%</div>
                  <div class="monitor-label">磁盘使用率</div>
                </div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="monitor-card">
                <div class="monitor-icon network">
                  <el-icon><component :is="iconComponents.Connection" /></el-icon>
                </div>
                <div class="monitor-content">
                  <div class="monitor-value">{{ monitorData.network }}ms</div>
                  <div class="monitor-label">网络延迟</div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>

        <!-- 操作日志 -->
        <div class="info-section">
          <h3 class="section-title">操作日志</h3>
          <el-table :data="operationLogs" style="width: 100%">
            <el-table-column prop="time" label="时间" width="180" />
            <el-table-column prop="operator" label="操作人" width="120" />
            <el-table-column prop="action" label="操作类型" width="120">
              <template #default="{ row }">
                <el-tag :type="getActionTagType(row.action)">
                  {{ row.action }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="操作描述" />
          </el-table>
        </div>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="editDialogVisible"
      title="编辑资产"
      width="800px"
      @close="resetEditForm"
    >
      <el-form
        ref="editFormRef"
        :model="editForm"
        :rules="rules"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资产名称" prop="name">
              <el-input v-model="editForm.name" placeholder="请输入资产名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="editForm.status" placeholder="请选择状态" style="width: 100%">
                <el-option label="在线" value="online" />
                <el-option label="离线" value="offline" />
                <el-option label="维护中" value="maintenance" />
                <el-option label="已报废" value="scrapped" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="负责人" prop="owner">
              <el-input v-model="editForm.owner" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置" prop="location">
              <el-input v-model="editForm.location" placeholder="请输入位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="editForm.description"
            type="textarea"
            :rows="3"
            placeholder="请输入资产描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  ArrowLeft,
  Edit,
  Delete,
  Cpu,
  Memo,
  FolderOpened,
  Connection
} from '@element-plus/icons-vue'

// 注册图标组件
const iconComponents = {
  ArrowLeft,
  Edit,
  Delete,
  Cpu,
  Memo,
  FolderOpened,
  Connection
}

// 路由相关
const route = useRoute()
const router = useRouter()

// 资产信息
const asset = reactive({
  id: '',
  name: '',
  type: '',
  ip: '',
  status: '',
  department: '',
  owner: '',
  location: '',
  serialNumber: '',
  description: '',
  createTime: '',
  updateTime: ''
})

// 监控数据
const monitorData = reactive({
  cpu: 0,
  memory: 0,
  disk: 0,
  network: 0
})

// 操作日志
const operationLogs = ref([
  {
    time: '2024-01-20 10:30:00',
    operator: '张三',
    action: '创建',
    description: '创建资产记录'
  },
  {
    time: '2024-01-21 14:20:00',
    operator: '李四',
    action: '更新',
    description: '更新资产状态为在线'
  },
  {
    time: '2024-01-22 09:15:00',
    operator: '王五',
    action: '维护',
    description: '进行系统维护'
  }
])

// 编辑相关
const editDialogVisible = ref(false)
const editFormRef = ref()
const editForm = reactive({
  name: '',
  status: '',
  owner: '',
  location: '',
  description: ''
})

// 表单验证规则
const rules = {
  name: [
    { required: true, message: '请输入资产名称', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ],
  owner: [
    { required: true, message: '请输入负责人', trigger: 'blur' }
  ]
}

// 工具方法
const getTypeTagType = (type: string) => {
  const typeMap: Record<string, string> = {
    server: 'primary',
    network: 'success',
    storage: 'warning',
    security: 'danger',
    software: 'info'
  }
  return typeMap[type] || 'info'
}

const getTypeLabel = (type: string) => {
  const typeMap: Record<string, string> = {
    server: '服务器',
    network: '网络设备',
    storage: '存储设备',
    security: '安全设备',
    software: '软件'
  }
  return typeMap[type] || type
}

const getStatusTagType = (status: string) => {
  const statusMap: Record<string, string> = {
    online: 'success',
    offline: 'danger',
    maintenance: 'warning',
    scrapped: 'info'
  }
  return statusMap[status] || 'info'
}

const getStatusLabel = (status: string) => {
  const statusMap: Record<string, string> = {
    online: '在线',
    offline: '离线',
    maintenance: '维护中',
    scrapped: '已报废'
  }
  return statusMap[status] || status
}

const getDepartmentLabel = (department: string) => {
  const departmentMap: Record<string, string> = {
    tech: '技术部',
    ops: '运维部',
    product: '产品部',
    marketing: '市场部'
  }
  return departmentMap[department] || department
}

const getActionTagType = (action: string) => {
  const actionMap: Record<string, string> = {
    创建: 'success',
    更新: 'primary',
    维护: 'warning',
    删除: 'danger'
  }
  return actionMap[action] || 'info'
}

// 方法
const goBack = () => {
  router.back()
}

const showEditDialog = () => {
  Object.assign(editForm, {
    name: asset.name,
    status: asset.status,
    owner: asset.owner,
    location: asset.location,
    description: asset.description
  })
  editDialogVisible.value = true
}

const resetEditForm = () => {
  editFormRef.value?.resetFields()
}

const handleEditSubmit = () => {
  editFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      // 更新资产信息
      Object.assign(asset, editForm)
      asset.updateTime = new Date().toLocaleString()

      // 添加操作日志
      operationLogs.value.unshift({
        time: new Date().toLocaleString(),
        operator: '当前用户',
        action: '更新',
        description: '更新资产信息'
      })

      ElMessage.success('更新成功')
      editDialogVisible.value = false
    }
  })
}

const handleDelete = () => {
  ElMessageBox.confirm(
    `确定要删除资产 "${asset.name}" 吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
    router.push('/cmdb/assets')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 获取资产详情
const getAssetDetail = () => {
  const assetId = route.params.id as string

  // 模拟获取资产详情
  const mockAsset = {
    id: assetId || 'ASSET001',
    name: 'Web服务器-01',
    type: 'server',
    ip: '192.168.1.100',
    status: 'online',
    department: 'tech',
    owner: '张三',
    location: '机房A-01',
    serialNumber: 'SN001234567',
    description: '主要用于Web应用部署',
    createTime: '2024-01-15 10:30:00',
    updateTime: '2024-01-20 15:45:00'
  }

  Object.assign(asset, mockAsset)

  // 模拟监控数据
  Object.assign(monitorData, {
    cpu: Math.floor(Math.random() * 100),
    memory: Math.floor(Math.random() * 100),
    disk: Math.floor(Math.random() * 100),
    network: Math.floor(Math.random() * 50)
  })
}

onMounted(() => {
  getAssetDetail()
})
</script>

<style scoped>
.asset-detail {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.asset-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--tech-text-primary);
}

.status-tag {
  margin-left: 10px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.detail-content {
  padding: 20px 0;
}

.info-section {
  margin-bottom: 30px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.02);
  border-radius: 12px;
  border: 1px solid rgba(120, 220, 255, 0.1);
}

.section-title {
  margin: 0 0 20px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--tech-accent-color);
  border-bottom: 2px solid rgba(120, 220, 255, 0.2);
  padding-bottom: 10px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.info-item label {
  min-width: 100px;
  font-weight: 500;
  color: var(--tech-text-secondary);
  margin-right: 15px;
}

.info-item span {
  color: var(--tech-text-primary);
  flex: 1;
}

.monitor-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 12px;
  border: 1px solid rgba(120, 220, 255, 0.1);
  transition: all 0.3s ease;
}

.monitor-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(120, 220, 255, 0.15);
}

.monitor-icon {
  width: 50px;
  height: 50px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 20px;
}

.monitor-icon.cpu {
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.2), rgba(64, 158, 255, 0.1));
  color: #409eff;
}

.monitor-icon.memory {
  background: linear-gradient(135deg, rgba(103, 194, 58, 0.2), rgba(103, 194, 58, 0.1));
  color: #67c23a;
}

.monitor-icon.disk {
  background: linear-gradient(135deg, rgba(230, 162, 60, 0.2), rgba(230, 162, 60, 0.1));
  color: #e6a23c;
}

.monitor-icon.network {
  background: linear-gradient(135deg, rgba(245, 108, 108, 0.2), rgba(245, 108, 108, 0.1));
  color: #f56c6c;
}

.monitor-content {
  flex: 1;
}

.monitor-value {
  font-size: 24px;
  font-weight: bold;
  color: var(--tech-accent-color);
  line-height: 1;
  margin-bottom: 5px;
}

.monitor-label {
  font-size: 12px;
  color: var(--tech-text-secondary);
  opacity: 0.8;
}

/* 表格样式 */
:deep(.el-table) {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(120, 220, 255, 0.1);
}

:deep(.el-table__header) {
  background: rgba(120, 220, 255, 0.05);
}

:deep(.el-table th) {
  background: rgba(120, 220, 255, 0.05);
  border-color: rgba(120, 220, 255, 0.1);
  color: var(--tech-text-primary);
}

:deep(.el-table td) {
  border-color: rgba(120, 220, 255, 0.1);
  color: var(--tech-text-secondary);
}

:deep(.el-table__row:hover) {
  background: rgba(120, 220, 255, 0.05);
}

/* 对话框样式 */
:deep(.el-dialog) {
  background: rgba(15, 25, 40, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(120, 220, 255, 0.2);
}

:deep(.el-dialog__header) {
  border-bottom: 1px solid rgba(120, 220, 255, 0.1);
  padding-bottom: 15px;
}

:deep(.el-dialog__title) {
  color: var(--tech-text-primary);
  font-weight: 600;
}
</style>
